package city.spring.modules.reception.service.impl;

import city.spring.modules.reception.entity.OldmanActiveEntity;
import city.spring.modules.reception.repository.OldmanActiveExtRepository;
import city.spring.modules.reception.repository.OldmanActiveRepository;
import city.spring.modules.reception.service.OldmanActiveService;
import city.spring.modules.reception.vo.OldmanActiveVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Service：活动记录
 *
 * @author pxm
 * @date 2020-06-30 15:17:02
 */
@CacheConfig(cacheNames = {OldmanActiveServiceImpl.CACHE_NAME})
@Transactional(rollbackFor = Throwable.class)
@Service
public class OldmanActiveServiceImpl extends ServiceImpl<OldmanActiveRepository, OldmanActiveEntity> implements OldmanActiveService {
    private final OldmanActiveExtRepository oldmanActiveExtRepository;

    public OldmanActiveServiceImpl(OldmanActiveExtRepository oldmanActiveExtRepository) {
        this.oldmanActiveExtRepository = oldmanActiveExtRepository;
    }

    @Override
    public void saveOldmanActive(OldmanActiveEntity entity) {
        if (!save(entity)) {
            throw new RuntimeException("保存信息失败");
        }
    }

    @Override
    public void updateOldmanActive(OldmanActiveEntity entity) {
        // 使用 updateById(entity); 修改数据时，将会修改 entity 对象中所有非null数据，如果某个字段为null，将会忽略该字段的修改
        boolean update = updateById(entity);
        if (!update) {
            throw new RuntimeException("修改信息失败");
        }
    }

    @Override
    public void deleteOldmanActive(String primaryKey) {
        removeById(primaryKey);
    }

    @Override
    public void deleteOldmanActive(List<String> primaryKeys) {
        removeByIds(primaryKeys);
    }

    @Override
    public OldmanActiveVO getActiveRecord(String id) {
        OldmanActiveEntity oldmanActiveEntity = getById(id);
        OldmanActiveVO oldmanActiveVO = new OldmanActiveVO(oldmanActiveEntity);
        // 加载排班计划的用户列表
        oldmanActiveVO.setOldmans(oldmanActiveExtRepository.getActiveOldmans(id));
        return oldmanActiveVO;
    }
}